package com.cy.dda.web.admin.user;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.comm.utils.ExportExcelUtils;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.user.UserAddAO;
import com.cy.dda.model.domain.user.User;
import com.cy.dda.model.enums.CommonStatus;
import com.cy.dda.model.query.user.DistributableUserListQuery;
import com.cy.dda.model.query.user.UserListAdminQuery;
import com.cy.dda.model.vo.user.DistributableUserListVO;
import com.cy.dda.model.vo.user.UserListAdminVO;
import com.cy.dda.service.user.UserService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-用户（催收员）管理
 *
 * @author: tanyilin
 * @create: 2018年10月31日 下午3:22:28
 */
@Slf4j
@RestController
@RequestMapping("/admin/user")
@Api(tags = "后台-用户（催收员）管理")
public class AdminUserController extends BaseController {

	@Autowired
	private UserService userService;
	
	@ApiOperation("新增用户（催收员）")
	@PostMapping("/do/add")
	public Result doAdd(@Validated UserAddAO userAddAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		User user = new User();
		BeanUtils.copyProperties(userAddAO, user);
		userService.addUser(user);
		return ok();
	}
	
	@ApiOperation("用户列表")
	@GetMapping("/list")
	public PageBean<UserListAdminVO> selectUserListAdmin(UserListAdminQuery query) {
		PageBean<UserListAdminVO> pageBean = userService.selectUserListAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("导出用户列表")
	@GetMapping("/list/export")
	public void listExport(UserListAdminQuery query, HttpServletRequest req, HttpServletResponse resp) {
		query.setPage(1);
		query.setLimit(5000);
		List<UserListAdminVO> data = this.selectUserListAdmin(query).getData();
		String title = "催收员用户列表";
		String[] headers = {"姓名", "手机号码", "委案总数", "进行中", "已完成", "催收日志", 
				"回款日志", "回款总额", "添加时间", "状态"};
		String[] fieldNames = {"userName", "mobile", "casesTotalCount", "casesProcessingCount", "casesEndCount", "collectionRecordCount", 
				"repayRecordCount", "repayAmount", "createTime", "statusText"};
		ExportExcelUtils<UserListAdminVO> ex = new ExportExcelUtils<>();
		try(OutputStream out = resp.getOutputStream()) {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("application/x-download");
			String fileName = CommonUtils.getCurrentTimeAndRandomNumber().concat(".xls");
			fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
			resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			ex.exportExcel(title, headers, fieldNames, data, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation("修改用户状态")
	@PostMapping("/update/status")
	public Result updateStatus(@RequestParam Long userId, @RequestParam CommonStatus status) {
		
		userService.updateStatus(userId, status);
		return ok();
	}
	
	@ApiOperation("查询可分配的催收员用户列表")
	@GetMapping("/select/distributable/list")
	public PageBean<DistributableUserListVO> selectDistributableUserList(DistributableUserListQuery query) {
		PageBean<DistributableUserListVO> pageBean = userService.selectDistributableUserList(query);
		return pageBean;
	}
	
}
